package com.controller;


import com.common.ApiRestResponse;
import com.common.Constant;
import com.common.Result;
import com.exception.MallException;
import com.exception.MallExceptionEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Product;
import com.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.UUID;

@Api(tags = "商品前台", value = "商品前台")
@CrossOrigin
@RestController
public class ProductController {

    @Autowired
    ProductService productService;

    @ApiOperation(value = "查询全部商品", httpMethod = "GET")
    @GetMapping(value = "/api/selectAllProducts")
    public Result selectAllProducts(){
        List<Product> products = productService.selectAllProducts();
        return Result.seccess(200, "查询成功", products);
    }

    @ApiOperation(value = "根据商品名称模糊查询", httpMethod = "GET")
    @GetMapping(value = "/api/selectProsByName")
    public Result selectProsByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "ctgname", required = false) String ctgname
    ){
        List<Product> products = productService.selectProsByName(name,ctgname);
        return Result.seccess(200, "查询成功", products);
    }

    @ApiOperation(value = "根据商品类别名称查询", httpMethod = "GET")
    @GetMapping(value = "/api/selectProsByCtg")
    public Result selectProsByCtg(
            @RequestParam(value = "ctgname", required = false) String ctgname
    ){

        List<Product> products = productService.selectProsByCtg(ctgname);
        return Result.seccess(200, "查询成功", products);
    }


    @PostMapping("/admin/upload/file")
    public ApiRestResponse upload(HttpServletRequest httpServletRequest,
                                  @RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //生成文件名称UUID
        UUID uuid = UUID.randomUUID();
        String newFileName = uuid.toString() + suffixName;
        //创建文件
        File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
        File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
        if (!fileDirectory.exists()) {
            if (!fileDirectory.mkdir()) {
                throw new MallException(MallExceptionEnum.MKDIR_FAILED);
            }
        }
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return ApiRestResponse
                    .success(getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/images/"
                            + newFileName);
        } catch (URISyntaxException e) {
            return ApiRestResponse.error(MallExceptionEnum.UPLOAD_FAILED);
        }
    }

    private URI getHost(URI uri) {
        URI effectiveURI;
        try {
            effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
                    null, null, null);
        } catch (URISyntaxException e) {
            effectiveURI = null;
        }
        return effectiveURI;
    }


}
